package com.taotao.common.enums;

import com.google.common.base.Joiner;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class CodeTextEnumUtils {


    public static String getDisplayName(Class<? extends CodeTextEnum> enumClass, Object value) {
        try {
            //反射调用枚举的values方法
            CodeTextEnum[] values = getValues(enumClass);
            if (null == values || 0 == values.length) {
                return null;
            }

            for (CodeTextEnum e : values) {
                if (Objects.equals(e.getCode(), value)) {
                    return e.getText();
                }
            }

        } catch (Exception e) {
            log.error("", e);
        }

        return null;
    }


    public static CodeTextEnum[] getValues(Class<? extends CodeTextEnum> enumClass) {
        try {
            return (CodeTextEnum[]) enumClass.getMethod("values").invoke(new Object(), new Object[]{});
        } catch (Exception e) {
            log.error("", e);
        }

        return null;
    }


    public static List<CodeTextEnumView> getCodeTextEnumViewList(Class<? extends CodeTextEnum> enumClass) {
        try {
            //反射调用枚举的values方法
            CodeTextEnum[] values = getValues(enumClass);
            if (null == values || 0 == values.length) {
                return null;
            }

            List<CodeTextEnumView> list = new ArrayList<>(values.length);


            for (CodeTextEnum e : values) {
                list.add(new CodeTextEnumView(e.getText(), e.getCode().toString()));
            }

            return list;
        } catch (Exception e) {
            log.error("", e);
        }

        return null;
    }

    public static String getDscr(Class<? extends CodeTextEnum> enumClass) {
        try {
            //反射调用枚举的values方法
            CodeTextEnum[] values = getValues(enumClass);
            if (null == values || 0 == values.length) {
                return null;
            }


            StringBuilder s = new StringBuilder();

            for (CodeTextEnum e : values) {
                s.append(",").append(e.getCode()).append("-").append(e.getText());
            }
            return s.toString().substring(1);

        } catch (Exception e) {
            log.error("", e);
        }

        return null;
    }


    public static String getCodes(Class<? extends CodeTextEnum> enumClass) {
        try {
            //反射调用枚举的values方法
            CodeTextEnum[] values = getValues(enumClass);
            if (null == values || 0 == values.length) {
                return null;
            }

            List list = new ArrayList<>(values.length);


            for (CodeTextEnum e : values) {
                list.add(e.getCode());
            }

            return Joiner.on(", ").join(list);
        } catch (Exception e) {
            log.error("", e);
        }

        return null;

    }

}
